/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.erp.server.DAO;

import com.erp.client.DAO.FileInfo;
import com.erp.client.DAO.UserBaseInf;
import com.erp.client.DAO.UserPasswordChange;
import com.erp.server.DB.DbConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class FileDao {

    public int insertFileData(List<FileInfo> fb) throws SQLException {
        int insertfile = 0;
        int i = 0;
        List<FileInfo> listfb = new ArrayList<FileInfo>();
        String sql = "INSERT INTO [apollo].[dbo].[sysFile] (fileName,myfile,userName,depName,fileType,empid) VALUES(?,?,?,?,?,?)";
        Connection conn = DbConn.getCon();

        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            for (i = 0; i <= fb.size(); i++) {
                ps.setString(1, fb.get(i).getFileName());
                ps.setString(2, fb.get(i).getMyfile());
                ps.setString(3, fb.get(i).getUserName());
                ps.setString(4, fb.get(i).getDepName());
                ps.setString(5, fb.get(i).getFileType());
                ps.setInt(6, fb.get(i).getEmpid());
                ps.executeUpdate();
            }
            
            insertfile = 1;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            insertfile = 10;
            e.printStackTrace();
        } finally {
            insertfile = 10;
            conn.close();
        }
        return insertfile;
        //
        /*String insertmyfile = "INSERT INTO [apollo].[dbo].[myFile] (userID,fileID) VALUES(?,?)";
        Connection connins = DbConn.getCon();
        try {
        PreparedStatement psin = connins.prepareStatement(insertmyfile);
        for (i = 0; i <= listfb.size(); i++) {
        psin.setInt(1, listfb.get(i).getEmpid());
        psin.setInt(2, listfb.get(i).getFileID());
        psin.executeUpdate();
        }
        insertfile = 3;
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        insertfile = 30;
        } finally {
        insertfile = 30;
        connins.close();
        }*/
    }
    public String selectFileID(String fb1,int fb2) throws SQLException {
        String fileString="";
        String sql = "SELECT fileName FROM [apollo].[dbo].[sysFile] where fileID=? and empid=?";
        Connection conn = DbConn.getCon();
        System.out.print("Fb1+Fb2:"+fb1+"  ; "+fb2);
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
                ps.setInt(1, Integer.parseInt(fb1));
                ps.setInt(2, fb2);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                fileString=rs.getString("fileName");                
                }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            conn.close();
        }
        return fileString;
    }

    public List<FileInfo> getFileByEmpid(int empid) throws SQLException {
        List<FileInfo> fileall = new ArrayList<FileInfo>();
        String sql = "select * from sysFile where delflag=1 and EmpID=? order by recDate DESC ";
        System.out.print("EmpID :"+empid);
        Connection conn = DbConn.getCon();
        try {

           PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, empid);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
                FileInfo u = new FileInfo();
                u.setFileID(rs.getInt("fileID"));
                u.setFileName(rs.getString("fileName"));
                u.setMyfile(rs.getString("myfile"));
                u.setUserName(rs.getString("userName"));
                u.setDepName(rs.getString("depName"));
                u.setFileType(rs.getString("fileType"));
                u.setRecDate(rs.getDate("recDate"));
                u.setDelFlag(rs.getBoolean("delFlag"));
                u.setEmpid(rs.getInt("EmpID"));
                fileall.add(u);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            conn.close();
        }
        System.out.print("filedao  : "+fileall.get(0).getFileName());
        return fileall;
    }
}
